package com.itheima.service.impl;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.CategoryMenuMapper;
import com.itheima.pojo.CategoryMenu;
import com.itheima.service.CategoryMenuService;
import com.itheima.utils.IdWorker;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class CategoryMenuServiceImpl implements CategoryMenuService {

    @Autowired
    private CategoryMenuMapper categoryMenuMapper;

    @Autowired
    private IdWorker idWorker;

    //获取菜单列表
    @Override
    public Page<CategoryMenu> findAll(Map<String, Object> categoryMenuVO) {
        Integer currentPage = Integer.parseInt(categoryMenuVO.get("currentPage").toString());
        Integer pageSize = Integer.parseInt(categoryMenuVO.get("pageSize").toString());
        PageHelper.startPage(currentPage,pageSize);
        Example example=this.createExample(categoryMenuVO);
        Page<CategoryMenu> categoryMenuPage = (Page<CategoryMenu>) categoryMenuMapper.selectByExample(example);
        return categoryMenuPage;
    }

    //增加菜单
    @Override
    @Transactional
    public void add(Map<String, Object> categoryMenuVO) {
        //将map转为comment对象
        String categoryMenuStr = JSON.toJSONString(categoryMenuVO);
        CategoryMenu categoryMenu = JSON.parseObject(categoryMenuStr, CategoryMenu.class);

        categoryMenu.setUid(String.valueOf(idWorker.nextId()));
        categoryMenu.setCreateTime(new Date());
        categoryMenu.setUpdateTime(new Date());
        //插入操作
        categoryMenuMapper.insertSelective(categoryMenu);
    }

    //编辑菜单
    @Override
    @Transactional
    public void edit(Map<String, Object> categoryMenuVO) {
        //将map转为comment对象
        String categoryMenuStr = JSON.toJSONString(categoryMenuVO);
        CategoryMenu categoryMenu = JSON.parseObject(categoryMenuStr, CategoryMenu.class);
        categoryMenu.setUpdateTime(new Date());
        //修改操作
        categoryMenuMapper.updateByPrimaryKey(categoryMenu);
    }

    //删除菜单
    @Override
    @Transactional
    public void delete(Map<String, Object> categoryMenuVO) {
        String uid = categoryMenuVO.get("uid").toString();
        Map<String,Object> map=new HashMap<>();
        map.put("parentUid",uid);
        Example example = this.createExample(map);
        List<CategoryMenu> categoryMenuList = categoryMenuMapper.selectByExample(example);
        if (categoryMenuList.size()==0){
            categoryMenuMapper.deleteByPrimaryKey(uid);
        }else {
            throw new RuntimeException("该菜单下还有子菜单！");
        }
    }

    //获取所有菜单列表
    @Override
    public List<CategoryMenu> getAllByKeyword(String keyword) {
        Map<String,Object> map=new HashMap<>();
        map.put("name",keyword);
        map.put("menuLevel",1);
        Example example=this.createExample(map);
        List<CategoryMenu> categoryMenuList = categoryMenuMapper.selectByExample(example);
        return this.getChildCategoryMenu(categoryMenuList);

    }

    //递归获取菜单列表
    public List<CategoryMenu> getChildCategoryMenu(List<CategoryMenu> categoryMenuList){
        for (CategoryMenu categoryMenu : categoryMenuList) {
            String parentUid = categoryMenu.getUid();
            Map<String,Object> map1=new HashMap<>();
            map1.put("parentUid",parentUid);
            Example example=this.createExample(map1);
            List<CategoryMenu> categoryMenus = categoryMenuMapper.selectByExample(example);
            if (categoryMenus!=null){
                categoryMenu.setChildCategoryMenu(categoryMenus);
                getChildCategoryMenu(categoryMenus);
            }
        }
        return categoryMenuList;
    }

    //获取所有二级菜单-按钮列表
    @Override
    public List<CategoryMenu> getAll2MenuLevelByKeyword(String keyword) {
        Map<String,Object> map=new HashMap<>();
        map.put("menuLevel",2);
        map.put("name",keyword);
        Example example=this.createExample(map);
        List<CategoryMenu> categoryMenuList = categoryMenuMapper.selectByExample(example);
        for (CategoryMenu categoryMenu : categoryMenuList) {
            //二级菜单uid
            String parentUid = categoryMenu.getUid();
            Map<String,Object> map1=new HashMap<>();
            map1.put("parentUid",parentUid);
            example=this.createExample(map1);
            List<CategoryMenu> categoryMenus = categoryMenuMapper.selectByExample(example);
            categoryMenu.setChildCategoryMenu(categoryMenus);
        }
        return categoryMenuList;
    }

    //置顶菜单
    @Override
    @Transactional
    public void stick(Map<String, Object> categoryMenuVO) {
        long sort=categoryMenuMapper.maxSort();
        String uid = categoryMenuVO.get("uid").toString();
        CategoryMenu categoryMenu = categoryMenuMapper.selectByPrimaryKey(uid);
        if (categoryMenu.getSort()==sort){
            throw new RuntimeException("该菜单已经再顶端!");
        }else {
            categoryMenu.setSort(sort+1);
            categoryMenu.setUpdateTime(new Date());
            categoryMenuMapper.updateByPrimaryKeySelective(categoryMenu);
        }
    }

    /**
     * 构建查询对象
     * @param searchMap
     * @return
     */
    private Example createExample(Map<String, Object> searchMap){
        Example example=new Example(CategoryMenu.class);
        Example.Criteria criteria = example.createCriteria();
        if(searchMap!=null){
            // 唯一uid
            if(searchMap.get("uid")!=null && !"".equals(searchMap.get("uid"))){
                criteria.andLike("uid","%"+searchMap.get("uid")+"%");
            }
            // 菜单名称
            if(searchMap.get("name")!=null && !"".equals(searchMap.get("name"))){
                criteria.andLike("name","%"+searchMap.get("name")+"%");
            }
            // 菜单级别
            if(searchMap.get("menuLevel")!=null){
                criteria.andEqualTo("menuLevel",searchMap.get("menuLevel"));
            }

            // 简介
            if(searchMap.get("summary")!=null && !"".equals(searchMap.get("summary"))){
                criteria.andLike("summary","%"+searchMap.get("summary")+"%");
            }

            // 父uid
            if(searchMap.get("parentUid")!=null && !"".equals(searchMap.get("parentUid"))){
                criteria.andEqualTo("parentUid",searchMap.get("parentUid"));
            }

            // url地址
            if(searchMap.get("url")!=null && !"".equals(searchMap.get("url"))){
                criteria.andLike("url","%"+searchMap.get("url")+"%");
            }

            // 图标
            if(searchMap.get("icon")!=null && !"".equals(searchMap.get("icon"))){
                criteria.andLike("icon","%"+searchMap.get("icon")+"%");
            }

            // 排序字段，越大越靠前
            if(searchMap.get("sort")!=null){
                criteria.andEqualTo("sort",searchMap.get("sort"));
            }

            // 状态
            if(searchMap.get("status")!=null){
                criteria.andEqualTo("status",searchMap.get("status"));
            }

            // 创建时间
            if(searchMap.get("createTime")!=null){
                criteria.andEqualTo("createTime",searchMap.get("createTime"));
            }

            // 更新时间
            if(searchMap.get("updateTime")!=null){
                criteria.andEqualTo("updateTime",searchMap.get("updateTime"));
            }

            // 是否显示 1:是 0:否
            if(searchMap.get("isShow")!=null){
                criteria.andEqualTo("isShow",searchMap.get("isShow"));
            }

            // 菜单类型 0: 菜单   1: 按钮
            if(searchMap.get("menuType")!=null){
                criteria.andEqualTo("menuType",searchMap.get("menuType"));
            }

        }
        return example;
    }
}
